layui.config({
    base: '../../static/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index' //主入口模块
}).use(['jquery','layer','form','table','laypage'], function() {
    var laypage = layui.laypage;
    var $ = layui.jquery;
    var form = layui.form;
    var table = layui.table;
    var layer = layui.layer; //独立版的layer无需执行这一句
    var role= $.cookie('role');
    var exportData;
    table.render({
            elem: '#userList'
            ,height: 'full-100'
            ,limit:10
            ,limits: [10,20,30,40,50,60,70,80,90,9999,9999999]
            ,async:false
            ,title:"用户信息表"
            ,page: true //开启分页
            ,url: '/user/listPage' //数据接口
            ,cellMinWidth: 80
            ,skin: 'row' //行边框风格
            ,even: true //开启隔行背景
            ,size: 'sm' //小尺寸的表格
            ,cols: [[ //表头
                {field: 'id', title: 'id',align: 'center'}
                ,{field: 'avatarUrl', title: '头像',align: 'center',templet:function (item) {
                    if(null==item.avatarUrl||""==item.avatarUrl){
                        return "";
                    }
                    //点击可以查看封面图
                    return '<div><img style="display: inline-block; width: 50%; height: 50%;" src='+item.avatarUrl+' lay-event="showAvatarUrl"></div>'
                }}
                ,{field: 'nickname', title: '用户昵称',  align: 'center'}
                ,{field: 'phone', title: '联系电话',  align: 'center'}
                // ,{field: 'status', title: '状态',  align: 'center',templet:"#switch"}
                ,{field: 'balance', title: '余额',align: 'center'}
                ,{field: 'faceStatus', title: '人脸注册状态',align: 'center',templet:function(item){
                        switch (item.faceStatus) {
                            case 0:
                                return '<span style="color:red ">未注册人脸</span>';
                            case 1:
                                return '<span style="color:green ">已注册</span>';
                            case 2:
                                return '<span style="color: grey">未识别</span>';
                            default:
                                return '';
                        }
                    }}
                ,{field: 'createtime', title: '注册时间',align: 'center'}
                ,{fixed: 'right',  title: '操作', width:300,  align:'center', toolbar: '#barDemo',align: 'center'}
            ]],
            done: function (res, curr, count) {
                exportData=res.data;
            }
        });

    form.on('switch(status-filter)', function(data){
        var swichid = data.elem.attributes['swichid'].nodeValue;
        var checked = data.elem.checked;
        var status;
        if (checked){
            status=1
        } else {
            status=0
        }
        var data = {
            id:swichid,
            status:status
        }
        $.ajax({
            url:"/user/addAndUpdate",
            type:"post",
            data:data,
            success:function (result) {
                if(result.code==0){
                    layer.msg(result.message, {icon: 1,time:2000,shade:0.2});
                }else {
                    layer.msg(result.message, {icon: 2,time:2000,shade:0.2});
                }
            }
        });
        form.render();
        return false;

    });


    // 监听行工具事件
    table.on('tool(userList)', function(obj){
        var data = obj.data,layEvent = obj.event;
        if(layEvent === 'del'){
            layer.confirm('确定删除名称为['+data.nickname+']的用户吗？', {
                btn: ['是', '否']
            }, function(index, layero){
                $.ajax({
                    url:"/user/delete",
                    type:"get",
                    data:{id:data.id},
                    success:function (result) {
                        if(result.code==0){
                            layer.msg('删除成功！', {icon: 1,time:2000,shade:0.2});
                            setInterval(function () {
                                window.location.reload();
                            },500);
                        }else {
                            layer.alert(result.message, {icon: 2,time:2000,shade:0.2});
                        }

                    }
                });
            });
        }else if(layEvent === 'faceReg'){
            layer.open({
                type: 2
                ,area:['100%', '100%']
                ,title: "人脸注册"
                // ,btn: ['提交','取消']
                ,content: 'faceReg.html?id='+data.id
                /*,yes: function(index, layero){
                    //监听提交
                    var iframeWin = window[layero.find('iframe')[0]['name']];//得到iframe页的窗口对象，执行iframe页的方法：
                    var submit=iframeWin.document.getElementById("submit");
                    submit.click();
                },
                end:function () {
                    $(".layui-laypage-btn").click();
                }*/
            });
        }else if(layEvent === 'edit'){
            layer.open({
                type: 2
                ,area:['100%', '100%']
                ,title: "修改用户信息"
                ,btn: ['提交','取消']
                ,content: 'add.html?id='+data.id
                ,yes: function(index, layero){
                    //监听提交
                    var iframeWin = window[layero.find('iframe')[0]['name']];//得到iframe页的窗口对象，执行iframe页的方法：
                    var submit=iframeWin.document.getElementById("submit");
                    submit.click();
                },
                end:function () {
                    $(".layui-laypage-btn").click();
                }
            });
        }else if(layEvent === 'showAvatarUrl'){
            var url=data.avatarUrl;
            layer.open({
                title:"查看头像"   //弹出层的顶部的标题
                ,type:1                   //弹出层的基本类型，默认为0，信息框
                ,area:["20%"]
                ,content:'<img src="'+url+'" style="width: 100%;height: 100%;"/>'		//访问id的content的div
                ,
            })
        }
    });




    $("#add").click(function () {
        layer.open({
            type: 2
            ,anim:1
            ,area:['100%', '100%']
            ,title: "添加用户信息"
            ,btn: ['保存','取消']
            ,content: 'add.html'
            ,yes: function(index, layero){
                // layer.close(index);//需要手动关闭窗口
                //监听提交
                var iframeWin = window[layero.find('iframe')[0]['name']];//得到iframe页的窗口对象，执行iframe页的方法：
                var submit=iframeWin.document.getElementById("submit");
                submit.click();

            }
        });
    });
    
    //点击人脸识别
    $("#faceRec").click(function () {
        layer.open({
            type: 2
            ,anim:1
            ,area:['100%', '100%']
            ,title: "人脸识别"
            ,content: 'faceReC.html'
        });

    });
  



})

